﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCSService
{
    public class AdvertyService
    {

        /// <summary>
        /// GetListAdverties
        /// </summary>
        /// <param name="languageID"></param>
        /// <param name="siteGuid"></param>
        /// <param name="active"></param>
        /// <returns></returns>
        public List<Adverty> GetListAdverties(int languageID, int siteGuid, bool? active)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Adverties
                            where p.LanguagesID == languageID && p.SiteGuid == siteGuid
                            select p);

               
                list = (from p in list
                        orderby p.Sort ascending
                        select p);
                return list.ToList();
            }
        }

        /// <summary>
        /// GetListAdvertiesBySearch
        /// </summary>
        /// <param name="languageID"></param>
        /// <param name="siteGuid"></param>
        /// <param name="AdvertiesName"></param>
        /// <param name="AdvertiesUrl"></param>
        /// <param name="AdvertiesTarget"></param>
        /// <param name="AdvertiesPromotion"></param>
        /// <param name="AdvertiesType"></param>
        /// <param name="AdvertiesView"></param>
        /// <param name="AdvertiesIP"></param>
        /// <param name="AdvertiesPageID"></param>
        /// <param name="AdvertiesActive"></param>
        /// <param name="Sort"></param>
        /// <returns></returns>
        public List<Adverty> GetListAdvertiesBySearch(int languageID, int siteGuid, string AdvertiesName, string AdvertiesUrl, string AdvertiesTarget, int? AdvertiesPromotion, int? AdvertiesType, int? AdvertiesView, string AdvertiesIP, string AdvertiesPageID, bool? AdvertiesActive, int? Sort)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Adverties
                            where p.LanguagesID == languageID && p.SiteGuid == siteGuid
                            select p);

                if (AdvertiesName != string.Empty && AdvertiesName != null)
                {
                    list = list.Where(p => p.AdvertiesName.Contains(AdvertiesName));
                }
                if (AdvertiesUrl != string.Empty && AdvertiesUrl != null)
                {
                    list = list.Where(p => p.AdvertiesUrl.Contains(AdvertiesUrl));
                }
                if (AdvertiesTarget != string.Empty && AdvertiesTarget != null)
                {
                    list = list.Where(p => p.AdvertiesTarget.Contains(AdvertiesTarget));
                }
                if (AdvertiesPromotion != null)
                {
                    list = list.Where(p => p.AdvertiesPromotion == AdvertiesPromotion);
                }
                if (AdvertiesType != null)
                {
                    list = list.Where(p => p.AdvertiesType == AdvertiesType);
                }
                if (AdvertiesView != null)
                {
                    list = list.Where(p => p.AdvertiesView==AdvertiesView);
                }
                if (AdvertiesIP != string.Empty && AdvertiesIP != null)
                {
                    list = list.Where(p => p.AdvertiesIP.Contains(AdvertiesIP));
                }
                if (AdvertiesPageID != string.Empty && AdvertiesPageID != null)
                {
                    list = list.Where(p => p.AdvertiesPageID.Contains(AdvertiesPageID));
                }
                if (AdvertiesActive != null)
                {
                    list = list.Where(p => p.AdvertiesActive == AdvertiesActive);
                }
                if (Sort != null)
                {
                    list = list.Where(p => p.Sort == Sort);
                }

                list = (from p in list
                        orderby p.Sort ascending
                        select p);

                return list.ToList();
            }
        }

        /// <summary>
        /// Create adverties
        /// </summary>
        /// <param name="adv"></param>
        /// <returns></returns>
        public int CreateAdverty(Adverty adv)
        {
            using (var data = new DataDataContext())
            {
                data.Adverties.InsertOnSubmit(adv);
                data.SubmitChanges();

                return adv.AdvertiesID;
            }
        }
        
        /// <summary>
        /// getadvertiesId
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       

       
        public Adverty GetAdvertyByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var adv = (from p in data.Adverties
                            where p.AdvertiesID == id
                            select p).FirstOrDefault();
                return adv;
            }
        }

        public Adverty GetAllAdverties()
        {
            using (var data = new DataDataContext())
            { 
                var adv=(from p in data.Adverties select p).FirstOrDefault();
                return adv;
            }
        }

       
        /// <summary>
        /// Get List Adverties by SiteGuid and LanguageId
        /// </summary>
        /// <param name="languageID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        /// 
        public List<Adverty> GetListAdvertiesBySiteGuidAndLanguageID(int languageID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Adverties
                            where p.LanguagesID == languageID && p.SiteGuid == siteGuid && p.AdvertiesActive == true
                            select p).ToList();
                return list;
            }
        }
      
     /// <summary>
        /// Get List Adverties for BackEnd (have Search)
     /// </summary>
     /// <param name="pageID"></param>
     /// <param name="languageID"></param>
     /// <param name="siteGuid"></param>
     /// <param name="AdvertiesName"></param>
     /// <param name="AdvertiesUrl"></param>
     /// <param name="AdvertiesTarget"></param>
     /// <param name="AdvertiesPromotion"></param>
     /// <param name="AdvertiesType"></param>
     /// <param name="AdvertiesView"></param>
     /// <param name="AdvertiesIP"></param>
     /// <param name="AdvertiesActive"></param>
     /// <param name="AdvSort"></param>
     /// <returns></returns>
        public List<Adverty> GetListAdvertiesSearch(string pageID, int languageID, int siteGuid, string AdvertiesName, string AdvertiesUrl, string AdvertiesTarget, int? AdvertiesPromotion, int? AdvertiesType, int? AdvertiesView, string AdvertiesIP, bool? AdvertiesActive, int? AdvSort)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Adverties
                            where p.LanguagesID == languageID && p.SiteGuid == siteGuid
                            select p);

                if (pageID!=string.Empty && pageID!=null)
                {
                    list = list.Where(p => p.AdvertiesPageID.Contains(pageID));
                }


                if (AdvertiesName != string.Empty && AdvertiesName != null)
                {
                    list = list.Where(p => p.AdvertiesName.Contains(AdvertiesUrl));
                }

                if (AdvertiesUrl != string.Empty && AdvertiesUrl != null)
                {
                    list = list.Where(p => p.AdvertiesUrl.Contains(AdvertiesUrl));
                }

                if (AdvertiesTarget != string.Empty && AdvertiesTarget != null)
                {
                    list = list.Where(p => p.AdvertiesTarget.Contains(AdvertiesTarget));
                }

                if (AdvertiesPromotion != null)
                {
                    list = list.Where(p => p.AdvertiesPromotion == AdvertiesPromotion);
                }

                if (AdvertiesType != null)
                {
                    list = list.Where(p => p.AdvertiesType == AdvertiesType);
                }

                if (AdvertiesView != null )
                {
                    list = list.Where(p => p.AdvertiesView == AdvertiesView);
                }

                if (AdvertiesIP != string.Empty && AdvertiesIP != null)
                {
                    list = list.Where(p => p.AdvertiesIP.Contains(AdvertiesIP));
                }
                
                if (AdvertiesActive != null)
                {
                    list = list.Where(p => p.AdvertiesActive == AdvertiesActive);
                }

                               
                list = (from p in list
                        orderby p.Sort ascending
                        select p);

                return list.ToList();
            }
        }

     
        /// <summary>
        /// Đệ quy list ID Adverties
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public StringBuilder GetListAdvertiesByID(ref StringBuilder sb, int id, int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Adverties
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid && p.AdvertiesID == id
                            select p.AdvertiesID).ToList();
                foreach (var item in type)
                {
                    sb.Append(item + ",");
                    if (type.Count() > 0)
                    {
                        GetListAdvertiesByID(ref sb, item, LanguagesID, siteGuid);
                    }
                }

                return sb;
            }
        }        
     
       
        /// <summary>
        /// Update Sort for Adverties
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newSort"></param>
        public void UpdateSortAdverties(int? id, int? newSort)
        {
            using (var data = new DataDataContext())
            {
                var adv = (from p in data.Adverties
                            where p.AdvertiesID == id
                            select p).FirstOrDefault();
                if (adv != null && newSort != null)
                {
                    if (adv.Sort != newSort )
                        adv.Sort = (int)newSort;

                    data.SubmitChanges();
                }
            }
        }
       
        /// <summary>
        /// Update Adverties
        /// </summary>
        /// <param name="adv"></param>
        public void UpdateAdverties(Adverty adv)
        {
            using (var data = new DataDataContext())
            {
               var adverty = (from p in data.Adverties
                                   where p.AdvertiesID == adv.AdvertiesID
                                   select p).SingleOrDefault();
                if (adv.AdvertiesName != string.Empty)
                {
                    adverty.AdvertiesName = adv.AdvertiesName;
                }

                if (adv.AdvertiesUrl != string.Empty)
                {
                    adverty.AdvertiesUrl = adv.AdvertiesUrl;
                }
                adverty.AdvertiesPromotion = adv.AdvertiesPromotion;
                adverty.AdvertiesType = adv.AdvertiesType;
                adverty.AdvertiesView = adv.AdvertiesView;


                if (adv.AdvertiesIP != null)
                {
                    adverty.AdvertiesIP = adv.AdvertiesIP;
                }

                if (adv.AdvertiesPageID != null)
                {
                    adverty.AdvertiesPageID = adv.AdvertiesPageID;
                }

                adverty.AdvertiesActive = adv.AdvertiesActive;
            

                if (adv.LanguagesID != null)
                {
                    adverty.LanguagesID = adv.LanguagesID;
                }

                data.SubmitChanges();
            }
        }
      
        /// <summary>
        /// Delete Adverties
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAdverties(int id)
        {
            using (var data = new DataDataContext())
            {
                var adv = (from p in data.Adverties
                            where p.AdvertiesID == id
                            select p).FirstOrDefault();
                data.Adverties.DeleteOnSubmit(adv);
                data.SubmitChanges();
            }
        }
       

        /// <summary>
        /// Get Count Adverties by siteGuid and Language
        /// </summary>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public int GetCountAdverties(int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Adverties
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid
                            select p).Count();


                return item;
            }
        }
      
     
    }
}
